package cn.cas.mango.util.RequestUtil;

import cn.cas.mango.service.ConfigService;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

// 根据id获取新闻
@Component
@Slf4j
public class GetIdsRequest {
  @Autowired
  private  ConfigService configService;

  public SearchRequest getIds(List<String> ids) {
    SearchRequest searchRequest = SearchRequest.of(
        x -> x.index(configService.getAliasIndex())
            .query(q -> q.terms(
                t -> t.field("key").terms(v -> v.value(ids.stream().map(FieldValue::of).collect(Collectors.toList())))))
            .source(s -> s.filter(f -> f.excludes("vector"))));
    return searchRequest;
  }
}
